package com.example.diary;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.example.data.DBManger;
import com.example.timecalculous.R;
import com.example.data.TimeData;

import java.util.Calendar;

/**
 * Created by ASUS on 2017/1/21.
 */

public class DiaryXiugai extends Activity {
    TimeData timedata;
    DBManger dbManger;
    int TIME_ID=0;
    Button btn1;
    EditText editText;
    Calendar calendar=Calendar.getInstance();
    int year,month,day;
    String year1=null,month1=null,day1=null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.diaryedit1);
        Bundle bundle=this.getIntent().getExtras();
        TIME_ID=bundle.getInt("diaryid");
        timedata=new TimeData(this);
        editText= (EditText) findViewById(R.id.edittextxiugai);
        //设置当前时间
        calendar.setTimeInMillis(System.currentTimeMillis());
        year=calendar.get(Calendar.YEAR);
        month=calendar.get(Calendar.MONTH);
        day=calendar.get(Calendar.DAY_OF_MONTH);
        year1=String.valueOf(year);
        month1=String.valueOf(month);
        day1=String.valueOf(day);
        btn1= (Button) findViewById(R.id.buttonxiugai);
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String str=editText.getText().toString();
                update1(TIME_ID,year1,month1,day1,str);
                timedata.close();
                Intent intent=new Intent(DiaryXiugai.this,DiaryHistories.class);
                startActivity(intent);
                DiaryXiugai.this.finish();
            }
        });
    }
    //修改操作
    public void update1(int id, String year,String month,String day,String textdata)
    {
        dbManger=new DBManger();
        SQLiteDatabase db = timedata.getWritableDatabase();
        String where = dbManger.TIME_ID1+ " = ?";
        String[] whereValue = { Integer.toString(id) };

        ContentValues cv = new ContentValues();
        cv.put(dbManger.YEAR, year);
        cv.put(dbManger.MONTH, month);
        cv.put(dbManger.DAY, day);
        cv.put(dbManger.DIARY, textdata);
        db.update(dbManger.TABLE_NAME1, cv, where, whereValue);
    }
}
